' //***************************************************************************
' // ***** Script Header *****
' //
' // Solution:  Solution Accelerator for Business Desktop Deployment
' // File:      ZTIBCDUtility.wsf
' //
' // Purpose:	Utility functions for bcd operations
' //
' // Usage:	 <script language="VBScript" src="ZTIBCDUtility.vbs"/>
' //
' // Microsoft Solution Version:  3.0.106
' // Microsoft Script Version:    3.0.106
' // Customer Build Version:      1.0.0
' // Customer Script Version:     1.0.0
' //
' // Microsoft History:
' // 4.0.0  tj   12/10/07  Creation
' //
' // Customer History:
' //
' // ***** End Header *****
' //***************************************************************************

Option Explicit

If Success = "" Then
	Public Const Success = 0
End if
If Failure = "" Then
	Public Const Failure = 1
End if

Dim iRetVal
Dim oShell, oFSO, oEnv
Dim sBcdEdit
Dim sErrorString

Set oShell = CreateObject("WScript.Shell")
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oEnv = oShell.Environment("PROCESS")

'//---------------------------------------------------------------------------
'//
'//  Function:	CreateNewRamDiskEntry()
'//
'//  Input:	
'//		sDescription - Description for BCD entry (example: "WinPE Boot")
'//		sPartition - Partion location for boot.wim (example: "D:")
'//     sPathToWim - Full path to WIM (example: "\sources\boot.wim")
'//		sDisplayOrder - Order selection (default "/addlast")
'//		bBootFirst - True/False to define next boot order. (-1/0)
'//		bDefaultBoot - True/False to set as default. (-1/0)
'//		iBootMenuTimeout - Default 30
'// 
'//  Return:	Success - 0
'//		Failure - 1
'//
'//  Purpose:	
'//		
'//---------------------------------------------------------------------------

Function CreateNewRamDiskEntry (sDescription, sPartition, sPathToWim, sDisplayOrder, bBootFirst, bDefaultBoot, iBootMenuTimeout)
 Dim iRetVal, sNewGuid, sCommand, sTemp
 Dim arrTemp,aGuidTemp
 
  If sPartition = "" Then
	sPartition = "C:"
  End If
 
  If sDescription = "" Then
    sDescription = "Custom Entry"
  End If

  If sDisplayOrder = "" Then
	sDisplayOrder = "/addlast"
  End If

  If iBootMenuTimeout = "" Then
	iBootMenuTimeout = 30
  End If

 ' // First create the new Boot Entry

   iRetVal = RunBcdEdit ("-create /d " & chr(34) & sDescription & chr(34) & " /application OSLOADER", True)

	If iRetVal <> Failure Then
		arrTemp = split(iRetVal, "{")
		aGuidTemp = split(arrTemp(1), "}")
		sNewGuid = "{" & aGuidTemp(0) & "}"
	Else
		CreateNewRamDiskEntry = iRetVal	  
		  Exit Function
	End If	 

' // Perform bulk command actions
 
	Dim arrCommands(10)
	arrCommands(0) = "-create {ramdiskoptions} -d " & chr(34) & "Ramdisk Device Options" & chr(34)
	arrCommands(1) = "-set {ramdiskoptions} ramdisksdidevice partition=" & sPartition
	arrCommands(2) = "-set {ramdiskoptions} ramdisksdipath \Boot\boot.sdi"
	arrCommands(3) = "-set " & sNewGuid & " device ramdisk=[" & sPartition & "]" & sPathToWim & ",{ramdiskoptions}"
	arrCommands(4) = "-set " & sNewGuid & " path \windows\system32\boot\winload.exe"
	arrCommands(5) = "-set " & sNewGuid & " osdevice ramdisk=[" & sPartition & "]" & sPathToWim & ",{ramdiskoptions} "
	arrCommands(6) = "-set " & sNewGuid & " systemroot \windows"
	arrCommands(7) = "-set " & sNewGuid & " winpe yes"
	arrCommands(8) = "-set " & sNewGuid & " detecthal yes"
	arrCommands(9) = "-displayorder " & sNewGuid & " " & sDisplayOrder
	arrCommands(10) = "-timeout " & iBootMenuTimeout
	 
	For Each sCommand in arrCommands

		iRetVal = RunBcdEdit(sCommand, False)
		
		If iRetVal = Failure Then
			sTemp = GetBcdError
			SetBcdError(sCommand & " ---- " & sTemp)
		
			CreateNewRamDiskEntry = iRetVal
			 Exit Function
		End If
		
	Next	

'// If required set next boot to the WIM

	If bBootFirst = -1 Then
	
		sCommand = "-bootsequence " & sNewGuid
		iRetVal = RunBcdEdit(sCommand, False)
		
		If iRetVal = Failure Then
			sTemp = GetBcdError
			SetBcdError(sCommand & " ---- " & sTemp)
		
			CreateNewRamDiskEntry = iRetVal
			 Exit Function
		End If		
	End If

'// If required set entry as {default}

	If bDefaultBoot = -1 Then
	
		sCommand = "-default " & sNewGuid
		iRetVal = RunBcdEdit(sCommand, False)
		
		If iRetVal = Failure Then
			sTemp = GetBcdError
			SetBcdError(sCommand & " ---- " & sTemp)
		
			CreateNewRamDiskEntry = iRetVal
			 Exit Function
		End If		
	End If	
	
	CreateNewRamDiskEntry = Success
End Function

'//---------------------------------------------------------------------------
'//
'//  Function:	RunBcdEdit()
'//
'//  Input:	
'//		sCommand - Command to be appended to bcdedit.exe
'//		bCapture - Boolean to indicate whether line capture should be performed.
'// 
'//  Return:	Success - 0
'//		Failure - 1
'//
'//  Purpose:	Private function
'//		
'//---------------------------------------------------------------------------

Function RunBcdEdit (sCommand, bCapture)
 Dim iRetVal, oExec, sLine,oExec1, sLine1,arrTemp1
 Dim re



	sBcdEdit = oEnv.Item("SystemRoot") & "\system32\bcdedit.exe"	
	
	If not oFSO.FileExists(sBcdEdit) Then
		SetBcdError ("Unable to locate bcdedit.exe")
		 RunBcdEdit = Failure
		  Exit Function
	End If


		
	sCommand = sBcdEdit & " " & sCommand


	Set oExec = oShell.Exec(sCommand)

		
	 sLine = oExec.StdOut.ReadLine

		

	if bCapture = True Then
		iRetVal = sLine
	Else
		iRetVal = Success
	End If


	RunBcdEdit = iRetVal
End Function


'//---------------------------------------------------------------------------
'//
'//  Function:	BackupStore()
'//
'//  Input:	
'//		sPath - Full path including file name of backup location. (example: "c:\backup\BackupStore.backup")
'// 
'//  Return:	Success - 0
'//		Failure - 1
'//
'//  Purpose:	Backup the BCD Store to designated location.
'//		
'//---------------------------------------------------------------------------

Function BackupStore (sPath)
  sPath = chr(34) & sPath & chr(34)
   iRetVal = RunBcdEdit ("/export " & sPath, False)

	BackupStore = iRetVal
End Function

Function CreateStore 
   iRetVal = RunBcdEdit ("/createstore c:\bcd", False)

	If iRetVal = Failure Then
		CreateStore = iRetVal	
		 Exit Function
	End If

   iRetVal = RunBcdEdit ("/import c:\bcd", False)

	If iRetVal = Failure Then
		CreateStore = iRetVal	
		 Exit Function
	End If



	iRetVal = RunBcdEdit ("-create {bootmgr} /d " & chr (34) & "Boot Manager" & chr(34), False)

	CreateStore = iRetVal
End Function

Function SetBcdError (sError)
	sErrorString = sError
End Function


Function GetBcdError ()
	GetBcdError =sErrorString
End Function

